package net.oschina.bilbodai.common.beanutil.bean.creator;

import net.oschina.bilbodai.common.beanutil.bean.code.EnumToStringApplier;
import net.oschina.bilbodai.common.beanutil.bean.code.IPropertyCodeApplier;
import net.oschina.bilbodai.common.beanutil.bean.core.PropertyDumpInfo;
import net.oschina.bilbodai.common.beanutil.bean.core.PropertyInfo;
import net.oschina.bilbodai.common.beanutil.exception.PropertyDumpException;

/**
 * @author daimao  Date: 16/5/30 Time: 上午11:34
 * @version $Id$
 */
public enum EnumToStringCreator implements IPropertyCodeApplierCreator {
    INSTANCE;

    @Override
    public IPropertyCodeApplier create(PropertyDumpInfo dumpInfo, PropertyInfo sourceInfo, PropertyInfo targetInfo) {
        Class<?> sourceType = sourceInfo.getFieldType();
        if (!Enum.class.isAssignableFrom(sourceType)) {
            throw new PropertyDumpException("SourceFieldType need to be enum，" + sourceInfo);
        }
        Class<?> targetType = targetInfo.getFieldType();
        if (!targetType.equals(String.class)) {
            throw new PropertyDumpException("TargetFieldType need to be String.class, " + targetInfo);
        }

        return new EnumToStringApplier(dumpInfo);
    }
}